//
//  NSString.m
//  Meng800
//
//  Created by 姚宗超 on 14-7-23.
//  Copyright (c) 2014年 点财网. All rights reserved.
//

#import "NSString+Business.h"

@implementation NSString (Business)
// 从IMSDK返回值中提取出手机号
- (NSString *)phoneNumTextFromIMSDKFormat
{
    return [self substringWithRange:NSMakeRange(0, 11)];
}

// timeStr "20140826144609"
- (NSDate*) dateFromIMSDKFormat
{
    NSInteger year = [[self substringToIndex:4] integerValue];
    NSInteger month = [[self substringWithRange:NSMakeRange(4, 2)] integerValue];
    NSInteger day = [[self substringWithRange:NSMakeRange(6, 2)] integerValue];
    NSInteger hour = [[self substringWithRange:NSMakeRange(8, 2)] integerValue];
    NSInteger minute = [[self substringWithRange:NSMakeRange(10, 2)] integerValue];
    NSInteger second = [[self substringWithRange:NSMakeRange(12, 2)] integerValue];
    NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    
    [comps setYear:year];
    [comps setMonth:month];
    [comps setDay:day];
    [comps setHour:hour];
    [comps setMinute:minute];
    [comps setSecond:second];
    
    return [gregorian dateFromComponents:comps];
}

// 根据人人网新鲜事日期的格式将字符串解析为NSDate
// 格式形如: 06-17 15:07
- (NSDate*) dateFromFeedFormat
{
	NSArray* se = [self componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"- :"]];
	if ([se count] == 4)
	{
		int month = [[se objectAtIndex:0] intValue];
		int day = [[se objectAtIndex:1] intValue];
		int hour = [[se objectAtIndex:2] intValue];
		int minute = [[se objectAtIndex:3] intValue];
		NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
		unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
		NSDate* date = [NSDate date];
		NSDateComponents* comps = [gregorian components:unitFlags fromDate:date];
		int year = [comps year];
		if (([comps month] < month) || ([comps month] == month && [comps day] < day))
			year--;
		
		NSDateComponents *comps1 = [[NSDateComponents alloc] init];
		[comps1 setYear:year];
		[comps1 setMonth:month];
		[comps1 setDay:day];
		[comps1 setHour:hour];
		[comps1 setMinute:minute];
		[comps1 setSecond:0];
		NSDate* ret = [gregorian dateFromComponents:comps1];
		return ret;
	}
	return nil;
}


// 根据人人网相册日期的格式将字符串解析为NSDate
// 格式形如: 2009-06-17
- (NSDate*) dateFromAlbumFormat
{
	NSArray* se = [self componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"-"]];
	if ([se count] == 3)
	{
		NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
		
		NSDateComponents *comps = [[NSDateComponents alloc] init];
		
		[comps setYear:[[se objectAtIndex:0] intValue]];
		[comps setMonth:[[se objectAtIndex:1] intValue]];
		[comps setDay:[[se objectAtIndex:2] intValue]];
		[comps setHour:0];
		[comps setMinute:0];
		[comps setSecond:0];
		
		NSDate* ret = [gregorian dateFromComponents:comps];
				
		return ret;
	}
	return nil;
}

- (NSDate *)dateWithDCWNSString
{
	NSArray* se = [self componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"- :"]];
	if ([se count] == 6)
	{
		NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
		
		NSDateComponents *comps = [[NSDateComponents alloc] init];
		
		[comps setYear:[[se objectAtIndex:0] intValue]];
		[comps setMonth:[[se objectAtIndex:1] intValue]];
		[comps setDay:[[se objectAtIndex:2] intValue]];
		[comps setHour:[[se objectAtIndex:3] intValue]];
		[comps setMinute:[[se objectAtIndex:4] intValue]];
		[comps setSecond:[[se objectAtIndex:5] intValue]];
		
		NSDate* ret = [gregorian dateFromComponents:comps];
        
		return ret;
	}
	return nil;
}

- (NSDate *)dateWithDCWNSStringProductDetail
{
    NSArray* se = [self componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"-"]];
	if ([se count] == 3)
	{
		NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
		
		NSDateComponents *comps = [[NSDateComponents alloc] init];
		
		[comps setYear:[[se objectAtIndex:0] intValue]];
		[comps setMonth:[[se objectAtIndex:1] intValue]];
		[comps setDay:[[se objectAtIndex:2] intValue]];
		
		NSDate* ret = [gregorian dateFromComponents:comps];
        
		return ret;
	}
	return nil;
}

#pragma mark - Meng800用
- (NSDate *)dateFromNewsTableInfo
{
    NSArray* se = [self componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"-"]];
    if ([se count] == 3)
    {
        NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        
        [comps setYear:[[se objectAtIndex:0] intValue]];
        [comps setMonth:[[se objectAtIndex:1] intValue]];
        [comps setDay:[[se objectAtIndex:2] intValue]];
        
        NSDate* ret = [gregorian dateFromComponents:comps];
        
        return ret;
    }
    return nil;
}

@end
